package ema4;

/**
 * @author Team TA's
 */

//muutettu kaikki mahdolliset start- ja endTimet StringeistÃ¤ inteiksi
public class Event {

	// FIXME
	public static final int[] VALID_START_TIMES = {8, 10, 12, 14, 16, 18};
	public static final int[] VALID_END_TIMES = {10, 12, 14, 16, 18, 20};
	private int startTime;
	private int endTime;
	private String location;
	private String title;

	public Event(int startTime, int endTime){
		this.setStartTime(startTime);
		this.setEndTime(endTime);
	}

	public Event(int startTime, int endTime, String title){
		this.setStartTime(startTime);
		this.setEndTime(endTime);
		this.setTitle(title);
	}

	public Event(int startTime, int endTime, String title, String location){
		this.setStartTime(startTime);
		this.setEndTime(endTime);
		this.setTitle(title);
		this.setLocation(location);
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public int getStartTime() {
		return startTime;
	}

	public void setStartTime(int startTime) {
		if (checkIfValid(startTime, VALID_START_TIMES)){
			this.startTime = startTime;
		}
		else {
			throw new IllegalArgumentException("Start time must be one of the following: " + getAllValidValues(VALID_START_TIMES));
		}
	}

	public int getEndTime() {
		return endTime;
	}

	public void setEndTime(int endTime) {
		if (checkIfValid(endTime, VALID_END_TIMES)){
			this.endTime = endTime;
		}
		else {
			throw new IllegalArgumentException("End time must be one of the following: "+ getAllValidValues(VALID_END_TIMES));
		}
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	// PRIVATES

	private String getAllValidValues(int[] listOfValids){
		String valids = "";
		for (int v : listOfValids){
			valids += "\""+v+"\", ";
		}
		return valids;
	}

	/**
	 * @param value
	 * @param allValids - Array of Strings that contains all valid Strings
	 * @return true if value is valid, ie.
	 * @return false if startTime didn't match a valid start time.
	 */
	private boolean checkIfValid(String value, String[] allValids){
		for (String valid : allValids){
			if (value.equalsIgnoreCase(valid)){
				return true;
			}
		}
		return false;
	}

        //UUSI
        private boolean checkIfValid(int value, int[] allValids){
		for (int valid : allValids){
			if (value == valid){
				return true;
			}
		}
		return false;
	}
}